package com.itheima.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Module;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.impl.ModuleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/system/module")
public class ModuleServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // 2021/10/18 start by chenggongbing
    // 模块增删改三层
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取参数
        String operation = request.getParameter("operation");
        if ("list".equals(operation)){
            list(request,response);
        }else if ("toAdd".equals(operation)){
            toAdd(request,response);
        }else if ("save".equals(operation)){
            save(request,response);
        }else if ("delete".equals(operation)){
            delete(request,response);
        }else if ("toEdit".equals(operation)){
            toEdit(request,response);
        }else if ("edit".equals(operation)){
            edit(request,response);
        }



    }

    //编辑功能
    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String parentId = request.getParameter("parentId");
        ModuleService moduleService = new ModuleServiceImpl();
        Module module1 = moduleService.findModuleById(parentId);
        String name = module1.getName();
        //获取参数
        String moduleId = request.getParameter("moduleId");
        String curl = request.getParameter("curl");
        String remark = request.getParameter("remark");


        //存到对象 传输到三层
        Module module = new Module();
        module.setModuleId(moduleId);
        module.setName(name);
        module.setCurl(curl);
        module.setRemark(remark);

        moduleService.updateModule(module);


        request.getRequestDispatcher(request.getContextPath()+"/system/module?operation=list").forward(request,response);
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收返回值
        Module module = findById(request, response);
        List<Module> moduleList = this.findAll(request, response);
        request.setAttribute("moduleList",moduleList);
        request.setAttribute("module",module);
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/system/module/update.jsp").forward(request,response);
    }

    private Module findById(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String moduleId = request.getParameter("id");

        //调用service层 通过id查询数据
        ModuleService moduleService = new ModuleServiceImpl();
        Module moduleById = moduleService.findModuleById(moduleId);

        return moduleById;
    }

    //删除功能
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String id = request.getParameter("id");
        ModuleService moduleService = new ModuleServiceImpl();
        moduleService.deleteModule(id);

        //删除完之后重新转发到查询方法
        request.getRequestDispatcher(request.getContextPath()+"/system/module?operation=list").forward(request,response);
    }
    //新建功能
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String parentId = request.getParameter("parentId");
        ModuleService moduleService = new ModuleServiceImpl();
        Module module1 = moduleService.findModuleById(parentId);
        String name = module1.getName();
        //获取输入的参数
        String parentName = request.getParameter("parentName");
        String curl = request.getParameter("curl");
        String remark = request.getParameter("remark");
        Module module = new Module();
        module.setName(name);
        //module.setParentId(parentId);
        module.setParentName(parentName);
        module.setCurl(curl);
        module.setRemark(remark);
        System.out.println(module);
        //调用service层 把输入的参数传过去

        moduleService.addModule(module);

        //传输数据给前端
        request.getRequestDispatcher(request.getContextPath()+"/system/module?operation=list").forward(request,response);


    }


    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Module> moduleList = findAll(request, response);
        request.setAttribute("moduleList",moduleList);
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/system/module/add.jsp").forward(request,response);

    }

    private List<Module> findAll(HttpServletRequest request, HttpServletResponse response) {

        ModuleService moduleService = new ModuleServiceImpl();
        List<Module> moduleList = moduleService.findAll();

        return moduleList;

    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String page = request.getParameter("page");
        ModuleService moduleService = new ModuleServiceImpl();
        PageInfo pageNum = moduleService.findByPage(page);

        //请求转发
        request.setAttribute("page",pageNum);
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/system/module/list.jsp").forward(request,response);

    }
}